package com.myblog.mapper;

import com.myblog.model.Article;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {

    @Select("select * from article order by id desc ")
    List<Article> selectAllArticle();

    @Select("select * from article where articleId = #{articleId}")
    Article selectArticleByArticleId(long articleId);

    @Select("select articleTitle from article where articleId = #{articleId}")
    String selectArticleTitleByArticleId(long articleId);

    @Select("select * from article where id = #{id}")
    Article selectArticleById(Integer id);

    //查找上一篇文章id，数据库最后一片文章的id
    long selectLastArticleId();

    void updataNextArticleId(@Param(value = "articleId")long articleId,@Param(value = "nextArticleId")long lastArticleId);

    void updataLastArticleId(@Param(value = "articleId")long articleId,@Param(value = "lastArticleId")long nextArticleId);

    void addArticle(Article article);

    @Delete("delete from article where id = #{id}")
    void deleteArticleById(@Param(value = "id") Integer id);

    @Select("select lastArticleId from article where id = #{id}")
    long getLastArticleIdById(Integer id);

    @Select("select nextArticleId from article where id = #{id}")
    long getNextArticleIdById(Integer id);

    void updataArticleById(Article article);

    @Select("select count(*) from article ")
    Integer selectNumOfArticle();

    @Select("select sum(likes) from article")
    Integer selectNumOfArticleLike();

    @Update("update article set likes = #{likes} where articleId = #{articleId}")
    void updateLikesByArticleId(Long articleId,@Param(value = "likes") Integer numOfArticleLikes);

    @Select("select likes from article where articleId = #{articleId}")
    Integer selectArticleLikesByArticleId(long articleId);

    @Update("update article set likes = likes+1 where articleId = #{articleId}")
    void updateLikeByArticleId(@Param(value = "articleId") long articleId);

    /**
     * 通过文章id获取文章点赞数，如果likes为空，则返回0
     * @param articleId
     * @return
     */
    @Select("select IFNULL(max(likes),0) from article where articleId = #{articleId}")
    Integer queryLikesByArticleId(@Param(value = "articleId") long articleId);

    @Select("select count(*) from article where articleCategories = #{categoryName}")
    Integer selectNumOfArticleByCategoryName(@Param("categoryName") String categoryName);

    @Select("select * from article where articleCategories = #{categoryName} order by id desc ")
    List<Article> selectArticleByCategoryName(String categoryName);

    @Select("select * from article where articleTitle like #{context}")
    List<Article> selectArticleByTitle(String context);

//    @Select("select * from article where articleContent like #{context}")
//    List<Article> selectArticleByContext(String context);
}
